//
//  STSettingViewController.m
//  赛拓
//
//  Created by 王磊 on 7/30/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STSettingViewController.h"
#import "STSettingTableView.h"
#import "STAccountSafeViewController.h"
#import "STRelevantViewController.h"
#import "STHelpAndFeedBackViewController.h"
@interface STSettingViewController ()<STBaseViewDelegate>
@property (nonatomic ,strong) STSettingTableView *tableView;
@end

@implementation STSettingViewController
- (STSettingTableView *)tableView {
    if (!_tableView) {
        _tableView = [STSettingTableView instanceWithFrame:self.view.bounds];
        
        _tableView.mDelegate = self;
        
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)initSubviews {
    [super initSubviews];
    
    self.title = @"设置";
    
    _tableView.cellType = STCellTypeSetting ;
}
- (void)loadData {
    
    [self.tableView updateData:@[@[@""],@[[self getTmpPics]],@[@"",@"v1.0"],@[@""]]];
}

- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    @WeakObj(self)
    if (indexPath.section == 0) {
        //账号与安全
        STAccountSafeViewController *accountSafe = [STAccountSafeViewController instance];
        
        [self.navigationController pushViewController:accountSafe animated:true];
    }else if (indexPath.section == 1) {
        ACActionSheet *sheet = [[ACActionSheet alloc]initWithTitle:@"是否清空缓存?" cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonRedTitles:@[@"确定"] actionSheetBlock:^(NSInteger buttonIndex) {
            
            if (buttonIndex == 0) {
                
                [selfWeak clearTmpPics];
                
            }
        }];
        [sheet show];
        
    }else if (indexPath.section == 2) {
        
        if (indexPath.row == 0) {
            STHelpAndFeedBackViewController *helpAndFeedBack = [STHelpAndFeedBackViewController instance];
            
            [self.navigationController pushViewController:helpAndFeedBack animated:true];
        }else {
            STRelevantViewController *relevant = [STRelevantViewController instance];
            
            [self.navigationController pushViewController:relevant animated:true];
        }
        
    }else {
        
        ACActionSheet *sheet = [[ACActionSheet alloc]initWithTitle:@"退出后将不会删除任何历史数据，下次登录依然可以使用本账号。" cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonRedTitles:@[@"退出登录"] actionSheetBlock:^(NSInteger buttonIndex) {
            
            if (buttonIndex == 0) {
                
                [selfWeak.mDelegate.accountBean clear];
                
                [STNotiCenter postNotificationName:ST_Logout_Noti object:nil];
            }
        }];
        [sheet show];
    }
}
- (void)clearTmpPics {
    
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    
    [self.tableView updateData:@[[self getTmpPics]] forIndexPath:indexPath];
}
- (NSString *)getTmpPics {
    float size = ((float)[[SDImageCache sharedImageCache] getSize]) / 1024 / 1024;
    return [NSString stringWithFormat:@"%.2fM",size];
}
@end
